package roadsuggester.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.location.Location;
import android.util.Log;


//Helper class to access locations database
public class UserLocationsDatabase extends SQLiteOpenHelper
{

	private static final int DATABASE_VERSION=3;
	private static final String TABLE_NAME="UserLocationsDatabase";
	private static final String TABLE_CREATE_SQL="CREATE TABLE "+TABLE_NAME+"(_id INTEGER PRIMARY KEY AUTOINCREMENT, label TEXT, address TEXT, latitude FLOAT, longitude FLOAT)";

	private SQLiteDatabase db;

	private static UserLocationsDatabase instance=null;

	private UserLocationsDatabase(Context context)
	{
		super(context, "locations", null, DATABASE_VERSION);

		this.db=getWritableDatabase();
	}
	
	//Singleton class
	public static UserLocationsDatabase getInstance(Context context)
	{
		if (instance==null)
		{
			instance=new UserLocationsDatabase(context);
		}
		return instance;
	}
	public void close()
	{
		//this.db.close();
	}

	//Add a location. If fails, return false
	public boolean addLocation(String label, String address, double latitude, double longitude)
	{
		ContentValues values=new ContentValues();

		values.put("label", label);
		values.put("address", address);
		values.put("latitude", latitude);
		values.put("longitude", longitude);

		// ask the database object to insert the new data
		try
		{
			db.insert(TABLE_NAME, null, values);
			return true;
		}
		catch(Exception e)
		{
			Log.e("DB ERROR", e.toString());
			e.printStackTrace();
			return false;
		}
		
	}
	
	//Delete a location by id
	public boolean deleteLocation(long id)
	{
		try
		{
			db.delete(TABLE_NAME, "_id="+id, null);
			return true;
		}
		catch(Exception e)
		{
			Log.e("DB ERROR", e.toString()); // prints the error message to the
												// log
			e.printStackTrace(); // prints the stack trace to the log
			return false;
		}
	}

	//Return all the locations
	public List<UserLocation> getLocations()
	{
		ArrayList<UserLocation> userLocations=new ArrayList<UserLocation>();


		try
		{
			Cursor cursor=db.query(TABLE_NAME, new String[] { "_id", "label", "address", "latitude", "longitude" }, null, null, null, null, null, null);

			do
			{
				UserLocation newLocation=new UserLocation(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getDouble(3), cursor.getDouble(4));
				userLocations.add(newLocation);
			} while(cursor.moveToNext());
			
			cursor.close();
		}
		catch(Exception e)
		{
			Log.e("DB ERROR", e.toString());
			e.printStackTrace();
		}


		return userLocations;
	}


	public Cursor getLocationsCursor()
	{
		Cursor cursor=db.query(TABLE_NAME, new String[] { "_id", "label", "address" }, null, null, null, null, null, null);
		return cursor;
	}


	//A wrapper for the locations
	public class UserLocation
	{
		public long id;
		public String label;
		public String address;
		public double latitude; 
		public double longitude;

		public UserLocation(int id, String label, String address, double latitude, double longitude)
		{
			this.id=id;
			this.label=label;
			this.address=address;
			this.latitude=latitude;
			this.longitude=longitude;
		}
		public Location toLocation()
		{
			Location l=new Location("");
			l.setLatitude(latitude);
			l.setLongitude(longitude);
			return l;
		}
	}


	@Override
	public void onCreate(SQLiteDatabase db)
	{
		db.execSQL(TABLE_CREATE_SQL);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
	{
		// TODO Auto-generated method stub

	}

	public void updateLocation(long locationID, String userLocationLabel, String userLocationAddress, double latitude, double longitude)
	{
		ContentValues values=new ContentValues();
		values.put("label", userLocationLabel);
		values.put("address", userLocationAddress);
		values.put("latitude", latitude);
		values.put("longitude", longitude);
		
		db.update(TABLE_NAME, values, "_id="+locationID, null);
		
	}

	public UserLocation getLocationByID(long locationID)
	{
		Cursor cursor=db.rawQuery("SELECT _id,label,address,latitude,longitude FROM "+TABLE_NAME+" where _id="+locationID,null);
		
		cursor.moveToFirst();
		UserLocation newLocation=new UserLocation(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getDouble(3), cursor.getDouble(4));
		cursor.close();
		return newLocation;
	}


}
